package com.yaorange.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yaorange.entity.Apply;
import com.yaorange.mapper.ApplyMapper;
import com.yaorange.service.ApplyService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description:
 * @company: yaorange
 * @author: 啊啊啊林
 * @version: 1.0
 * @create: 2021-09-26 19:44:28
 */
@Service
@Transactional//事务
public class ApplyServiceImpl implements ApplyService {
    @Resource
    private ApplyMapper applyMapper;
    @Override
    public List<Apply> selectApplyDate(Integer state) {

        List<Apply> applies = applyMapper.selectByXml(state);
        return applies;
    }

    @Override
    public int updateApplyState(Integer applyId,Integer stateId) {
        int i = applyMapper.updateApplyState(applyId, stateId);

        return i;
    }

    @Override
    public PageInfo<Apply> selectApplyByPage(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Apply> applies = applyMapper.selectByXml(0);

        return new PageInfo<>(applies);
    }

    @Override
    public PageInfo<Apply> selectApplyByPageOne(Integer userId) {
        PageHelper.startPage(1, 10);
        List<Apply> applies = applyMapper.selectByXmlUser(0, userId);

        return new PageInfo<>(applies);
    }

    @Override
    public int insertNewApply(Apply apply) {
        int i = applyMapper.insertSelective(apply);
        return i;
    }

    @Override
    public PageInfo<Apply> selectApplyList(Integer userId, Integer currentPage, String goodsName, Integer typeId) {
        PageHelper.startPage(currentPage, 3);
        List<Apply> applies = applyMapper.selectApply(userId,"%"+goodsName+"%",typeId);
        System.out.println(applies);

        return new PageInfo<>(applies);

    }

    @Override
    public int deleteApplyByApplyId(Integer applyId) {
        int i = applyMapper.deleteByPrimaryKey(applyId);
        return i;
    }

    @Override
    public int updateApplyPerson(Integer applyId, String applyReason, Integer applyNum) {
        Apply apply = new Apply();
        apply.setApplyId(applyId);
        apply.setApplyReason(applyReason);
        apply.setApplyNum(applyNum);

        int i = applyMapper.updateByPrimaryKeySelective(apply);
        return i;

    }

    @Override
    public int updateApplyStateMany(Integer[] str) {
        for (int i = 0; i < str.length; i++) {
            applyMapper.updateApplyState(str[i], 2);
        }
        return 1;

    }


}
